using System;
using System.Text;
using System.Collections.Generic;
using Sce.PlayStation.HighLevel.UI;

namespace VitaReaderLibPS
{

	public class MessageBox : MessageDialog
	{
		public delegate void OnOKDelegate(object target);
		
		internal ReaderScene  mScene;
		internal OnOKDelegate mDelegate;
		internal object 	  mDelegateTarget;
		internal Widget		  mLastKeyWidget;
		
		
		public MessageBox(String title,String message,ReaderScene scene)
			:base( MessageDialogStyle.Ok, title, message )
		{
			mScene = scene;
			mDelegate = null;
			
			this.BackgroundStyle = DialogBackgroundStyle.Custom;
			this.CustomBackgroundColor = mScene.DialogBackgroundColor;
			
			this.ChangeChild(this);
			
			this.KeyEventReceived += HandleMessageBoxKey;
			this.Hidden += HandleMessageBoxHiden;
			mLastKeyWidget = UISystem.KeyReceiverWidget;
			UISystem.KeyReceiverWidget = this;
		}
		
		public MessageBox(String title,String message,ReaderScene scene, MessageDialogStyle style,OnOKDelegate deleg,object o)
			:base( style, title, message )
		{
			mScene = scene;
			mDelegate = deleg;
			mDelegateTarget = o;
			this.BackgroundStyle = DialogBackgroundStyle.Custom;
			this.CustomBackgroundColor = scene.DialogBackgroundColor;
			
			this.ChangeChild(this);
			
			this.KeyEventReceived += HandleMessageBoxKey;
			//this.ButtonPressed += HandleMessageBoxButton;
			this.Hidden += HandleMessageBoxHiden;
			mLastKeyWidget = UISystem.KeyReceiverWidget;
			UISystem.KeyReceiverWidget = this;
		}
		
		void ChangeChild(Widget child)
		{
			ContainerWidget container = child as ContainerWidget;
			if( container != null )
			{
				foreach( Widget subWidget in container.Children )
				{
					ChangeChild(subWidget);
				}
			}
			else
			{
				Button button = child as Button;
				if( button != null )
				{
					button.Style = ButtonStyle.Custom;
					button.CustomImage = mScene.ButtonImage;
					button.TextColor = mScene.ButtonTextColor;
					button.TextFont = mScene.ButtonTextFont;
					//TODO: hack button text?
				}
				else
				{
					Label label = child as Label;
					if( label != null )
						label.TextColor = mScene.TitleColor;
				}
			}
		}
		
		void HandleMessageBoxKey(object sender, KeyEventArgs e)
		{
			if( e.KeyEventType == KeyEventType.Up 
			   && (e.KeyType == KeyType.Enter || e.KeyType == KeyType.Back )
			   )
			{
				if( e.KeyType == KeyType.Enter )
				{
					this.Result = DialogResult.Ok;
				}
				else
					this.Result = DialogResult.Cancel;
				
				//hide will auto trigger the event of button press,calls HandleMessageBoxButton
				this.Hide();
			}
		}
		
		/*
		void HandleMessageBoxButton(object sender,MessageDialogButtonEventArgs e)
		{
			if( e.Result == MessageDialogResult.Ok && mDelegate != null )
				mDelegate(mDelegateTarget);
		}
		*/
		void HandleMessageBoxHiden(object sender, DialogEventArgs e)
		{
			UISystem.KeyReceiverWidget = mLastKeyWidget;
			
			if( e.Result == DialogResult.Ok && mDelegate != null )
				mDelegate(mDelegateTarget);
		}
	}//class 
}